package com.tqhh.meimei.service;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    public static void main(String[] args) {
        String strImg = "<img src=\"/img/jruoerqi/1.jpg\" alt=\"宝马小妹妩媚自拍\" /><br/><img src=\"/img/jruoerqi/2.jpg\" alt=\"宝马小妹妩媚自拍\" /><br/><img src=\"/img/jruoerqi/3.jpg\" alt=\"宝马小妹妩媚自拍\" /><br/><img src=\"/img/jruoerqi/4.jpg\" alt=\"宝马小妹妩媚自拍\" /><br/><img src=\"/img/jruoerqi/5.jpg\" alt=\"宝马小妹妩媚自拍\" /><br/><img src=\"/img/jruoerqi/6.jpg\" alt=\"宝马小妹妩媚自拍\" /><br/>";
        getImgStr(strImg);

        ArrayList<Integer> list   = new ArrayList<Integer>();
        int radom=0;
//        for (int i = 0; i < 5; i++) {
//            radom = (int)(1+Math.random()*(10-1+1));
//            System.out.println(radom);
//            list.add(i,radom);
//        }
        System.out.println("结果："+getCids());

        get();

    }

    public static void get(){
        Random random = new Random();
        List<Integer> history = new ArrayList<>();
        history.add(1);
        history.add(5);
        history.add(8);
        history.add(7);
        Set<Integer> set = new HashSet<>();
        while(set.size() <= 5)
        {
            int index = random.nextInt(10);
            if (!history.contains(index)) {
                set.add(index);
            }
        }
        System.out.println(set);
    }
    public static Long getCids(){
        List<Long> history = new ArrayList<>();
        history.add(1l);
        history.add(5l);
        history.add(8l);
        history.add(7l);
        long radom = (long) (1 + Math.random() * (10 - 1 + 1));
        System.out.println("每次的随机数："+radom);
        if (!history.contains(radom)){
            history.add(radom);
            return radom;
        }else {
            return getCids();
        }
    }
    public static Set<String> getImgStr(String htmlStr) {
        Set<String> pics = new HashSet<>();
        String img = "";
        Pattern p_image;
        Matcher m_image;
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile
                (regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            // 得到<img />数据
            img = m_image.group();
            // 匹配<img>中的src数据
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                pics.add(m.group(1));
                System.out.println(m.group(1));
            }
        }
        return pics;
    }

}


